package com.siwei.mes.handler.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.CheckQuickConfig;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.experiment.concreteAdmixture.*;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.experiment.CheckBatchConfigMapper;
import com.siwei.mes.mapper.experiment.CheckQuickConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 外加剂对应实现类
 *
 * @Description: 外加剂对应实现类
 * @Version: 1.0
 * @author: yangke
 * @date: 2024/6/05 12:14
 */
@Slf4j
@Component("6")
public class ConcreteAdmixtureServiceimpl extends AbstractExperimentFactoryServiceImpl {
    @Resource
    private CheckQuickConfigMapper checkQuickConfigMapper;
    @Resource
    private CheckBatchConfigMapper checkBatchConfigMapper;

    @Override
    public void checkIsBatchFinish(Experiment experiment) {
        log.info("我是外加剂 我执行了");
    }


    @Override
    public String checkIsMustToExperimentDetail(List<ExperimentDetail> detailList) {
        if (detailList.isEmpty()) {
            return null;
        }
        for (ExperimentDetail experimentDetail : detailList) {
            Integer checkType = experimentDetail.getCheckType();//检查类型 1-快检 2-批检
            String testProjectCode = experimentDetail.getTestProjectCode();
            JSONObject objJson = experimentDetail.getObjJson();
            if (checkType == null || testProjectCode == null || objJson == null) {
                return null;
            }
            Integer isMust = null; //1-必检
            Integer isUploadMust = null;//图片是否必检 1-必检
            if (checkType == 1) {
                CheckQuickConfig checkQuickConfig = checkQuickConfigMapper.findInfoByTestProjectCode(testProjectCode);
                if (checkQuickConfig != null) {
                    isMust = checkQuickConfig.getIsMust();
                    isUploadMust = checkQuickConfig.getIsUploadImg();
                }
            } else if (checkType == 2) {
                CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.findInfoByTestProjectCode(testProjectCode);
                if (checkBatchConfig != null) {
                    isMust = checkBatchConfig.getIsMust();
                    isUploadMust = checkBatchConfig.getIsUploadImg();
                }
            }
            String msg = null;
            if (isMust != null && isMust == 1) {
                msg = this.checkIsMustToEntity(objJson, testProjectCode);
            }
            /**图片校验 目前这里只有批检的都是在每一条detail中 则立马就校验  如果有快检需要的则加入到对应集合中*/
            if (StringUtils.isBlank(msg) && isUploadMust != null && isUploadMust == 1) {
                msg = this.checkIsUploadImg(experimentDetail.getObjImg(), testProjectCode);
            }
            if (StringUtils.isNotBlank(msg)) {
                return msg;
            }
        }
        return null;
    }

    /**
     * 根据对应的试验类型判断是否需要必填的
     *
     * @param objJson
     * @param testProjectCode
     */
    private String checkIsMustToEntity(JSONObject objJson, String testProjectCode) {
        TestProjectEnum testProjectEnum = TestProjectEnum.getName(testProjectCode);
        if (testProjectEnum == null) {
            log.info("暂未配置该类型的必填校验，类型：" + testProjectCode);
            return null;
        }

        switch (testProjectEnum) {
            case CONCRETE_ADMIXTURE_PARAM_WJJ_PHZ:
                return ConcreteAdmixturePhz.checkIsMust(objJson, testProjectEnum.getName());
            case CONCRETE_ADMIXTURE_PARAM_WJJ_HGL:
                return ConcreteAdmixtureHgl.checkIsMust(objJson, testProjectEnum.getName());
            case CONCRETE_ADMIXTURE_PARAM_WJJ_MD:
                return ConcreteAdmixtureMd.checkIsMust(objJson, testProjectEnum.getName());
            case CONCRETE_ADMIXTURE_PARAM_WJJ_NJSJZC:
                return ConcreteAdmixtureNjsjzc.checkIsMust(objJson, testProjectEnum.getName());
            case CONCRETE_ADMIXTURE_PARAM_WJJ_QSLB:
                return ConcreteAdmixtureQslb.checkIsMust(objJson, testProjectEnum.getName());
            case CONCRETE_ADMIXTURE_PARAM_WJJ_TLD1HJSBHL:
                return ConcreteAdmixtureTld1hjsbhl.checkIsMust(objJson, testProjectEnum.getName());
            case CONCRETE_ADMIXTURE_PARAM_WJJ_JSL:
                return ConcreteAdmixtureJSL.checkIsMust(objJson, testProjectEnum.getName());
            default:
                return null;
        }
    }
}
